package com.springcloud.zuuldemo.controller

import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cloud.client.loadbalancer.LoadBalanced
import org.springframework.security.oauth2.client.OAuth2RestTemplate

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController

@RestController
class LocalController {

    @Autowired
    @LoadBalanced
    OAuth2RestTemplate template

    @GetMapping('/local/{v}')
    def local(@PathVariable String v)
    {
        template.getForObject('http://p1/get',String.class)
    }

        //JWT的创建与解析
    static void main(String[] args) {

        //测试token的生成
        Map<String,String> map=new HashMap<>()
        map.put('name','ding')
        def token=Jwts.builder()
            .setClaims(map)    //私有claim
            .setExpiration(new Date(System.currentTimeMillis()+1000*60*5))    //公共claim,到期时间
            .signWith(SignatureAlgorithm.HS256,'qzofqnp;osnein-fe')  //加密算法以及秘钥
            .compact()


        println(token)

        def claims=Jwts.parser()
            .setSigningKey('qzofqnp;osnein-fe')
            .parseClaimsJws(token)
            .getBody()

        println(claims.getExpiration())
        println(claims.get('name'))
    }

}
